<% @page_title = "Campaign Statistics" %>

<body>
  <div id="stat-info" class="wrapper" style="position:relative">
    <div class='colorBox' style="width:800px;height:100px;top:0px;left:0px;background-color: #9edae5"> 
      <h3 class='textItems' style="color:grey;">Campaign Name: </h2>
      <h2 id='campaign_name' class='textItems' style="color:black;">Not Loaded.</h2>
    </div>
    <div class='colorBox' style="width:395px;height:100px;top:110px;left:0px;background-color: #9ecae1"> 
      <h3 class='textItems' style="color:grey;">Active: </h2>
      <h2 id='active' class='textItems' style="color:black;">-</h2>
    </div>
    <div class='colorBox' style="width:395px;height:100px;top:220px;left:0px;background-color: #9edae5"> 
      <h3 class='textItems' style="color:grey;">Time Elapsed: </h2>
      <h2 id='time' class='textItems' style="color:black;">-</h2>
    </div>
    <div class='colorBox' style="width:395px;height:100px;top:330px;left:0px;background-color: #9ecae1"> 
      <h3 class='textItems' style="color:grey;">Template Used: </h2>
      <h2 id='template_name' class='textItems' style="color:black;">-</h2>
    </div>
    <div id="chartDiv" class='colorBox' style="width:395px;height:320px;top:110px;left:405px;background-color: #FFF;border: 0px">
    </div>
    <div class='colorBox' style="width:192.5px;height:100px;top:440px;left:0px;margin-bottom:20px;background-color: #9edae5">
      <h3 class='textItems' style="color:grey;">Sent: </h2>
      <h2 id='sent' class='textItems' style="color:black;">-</h2>
    </div>
    <div class='colorBox' style="width:192.5px;height:100px;top:440px;left:202.5px;background-color: #9ecae1">
      <h3 class='textItems' style="color:grey;">Opened: </h2>
      <h2 id='opened' class='textItems' style="color:black;">-</h2>
    </div>
    <div class='colorBox' style="width:192.5px;height:100px;top:550px;left:0px;background-color: #9ecae1">
      <h3 class='textItems' style="color:grey;">Clicked: </h2>
      <h2 id='clicked' class='textItems' style="color:black;">-</h2>
    </div>
    <div class='colorBox' style="width:192.5px;height:100px;top:550px;left:202.5px;background-color: #9edae5">
      <h3 class='textItems' style="color:grey;">Click Rate: </h2>
      <h2 id='success_rate' class='textItems' style="color:black;">-</h2>
    </div>
    <div class='colorBox' style="width:192.5px;height:100px;top:440px;left:405px;background-color: #9edae5">
      <h3 class='textItems' style="color:grey;">Harvested: </h2>
      <h2 id='harvested' class='textItems' style="color:black;">-</h2>
    </div>
    <div class='colorBox' style="width:192.5px;height:100px;top:550px;left:405px;background-color: #9ecae1">
      <h3 class='textItems' style="color:grey;">Passwords: </h2>
      <h2 id='passwords' class='textItems' style="color:black;">-</h2>
    </div>
    <div class='colorBox' style="width:192.5px;height:100px;top:440px;left:607.5px;background-color: #9ecae1">
      <h3 class='textItems' style="color:grey;">Harvest Rate: </h2>
      <h2 id='harvest_rate' class='textItems' style="color:black;">-</h2>
    </div>
    <div>
      <table id="victims-summary-table" class="table-hover table-condense">
        <thead>
          <tr>
            <th>Victim UID</th>
            <th>Email Address</th>
            <th>Email Sent?</th>
            <th>Email Opened?</th>
            <th>Link Clicked?</th>
            <th>Password Seen?</th>
            <th>Time Visited</th>
          </tr>
        </thead>
        <tbody>

        </tbody>
      </table>
    </div>
  </div>

  <!-- Modal Button -->
  <div id="report-options" class="pull-right">
    <button class="btn btn-default" data-toggle="modal" data-target="#display-report-options">
      More Options
    </button>
  </div>

  <!-- Modal -->
  <%= render partial: 'more_options' %>

</body>
<script type="text/javascript" >
var w = 395,
    h = 320,
    r =  Math.min(w, h) / 2.2,
    inner =  (Math.min(w, h) / 4),
    color = d3.scale.category20c();

data = [{"label":"Clicked", "value":1},
        {"label":"No Action", "value":1}]


var total = d3.sum(data, function(d) {
    return d3.sum(d3.values(d));
});

var vis = d3.select("#chartDiv")
    .append("svg:svg")
    .data([data])
        .attr("width", w)
        .attr("height", h)
        .append("svg:g")
        .attr("transform", "translate(" + w / 2 + "," + h / 2 + ")");
        //.attr("transform", "translate(" + r * 1.1 + "," + r * 1.1 + ")")

var textTop = vis.append("text")
    .attr("dy", ".35em")
    .style("text-anchor", "middle")
    .attr("class", "textTop")
    .text( "TOTAL" )
    .attr("y", -10),
textBottom = vis.append("text")
    .attr("dy", ".35em")
    .style("text-anchor", "middle")
    .attr("class", "textBottom")
    .text(total.toFixed(2))
    .attr("y", 10);

var arc = d3.svg.arc()
    .innerRadius(inner)
    .outerRadius(r);

var arcOver = d3.svg.arc()
    .innerRadius(inner + 5)
    .outerRadius(r + 5);
 
/*var pie = d3.layout.pie()
    .value(function(d) { return d.value; });*/
 
var pie = d3.layout.pie()
    .sort(null)
    .value(function (d) {
    return d.value;
});

var arcs = vis.selectAll("g.slice")
    .data(pie(data))
    .enter()
        .append("svg:g")
            .attr("class", "slice");

var path = arcs.append("svg:path")
    .attr("fill", function(d, i) { return color(i); } )
    .attr("d", arc);

path.on("mouseover", function(d) {
                console.log(d);
                d3.select(this).select("path").transition()
                    .duration(200)
                    .attr("d", arcOver)
                
                textTop.text(d.data.label)
                    .attr("y", -10);
                textBottom.text(d.data.value)
                    .attr("y", 10);
            })
            .on("mouseout", function(d) {
                d3.select(this).select("path").transition()
                    .duration(100)
                    .attr("d", arc);
                
                textTop.text( "TOTAL" )
                    .attr("y", -10);
                textBottom.text(totalData);
            })


 path.transition()
      .duration(500)
      //.attr("fill", function(d, i) { return color(d.data.actionLabel); })
      .attr("d", arc)
      .each(function(d) { this._current = d; }); // store the initial angles

function change(da){
    path.data(pie(da));
    path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs
    textTop.text( "TOTAL" )
                    .attr("y", -10);
    textBottom.text(totalData);

}

function arcTween(a) {
  var i = d3.interpolate(this._current, a);
  this._current = i(0);
  return function(t) {
    return arc(i(t));
  };
}

function totalData() {
  var total = 0;
  d3.selectAll("path").data().map( function(item) {
    total += item.data.value;
  });
  return total;
  /*
  d3.selectAll("path").data().map( function(item) {
    total += item.data.num;
  });  
  textBottom.text(total).attr("y", 10);
  */
}
function initBar() {
var width = 395,
    height = 200;
    
data = [
        {"Time":"0h-1h", "Clicks":5},
        {"Time":"1h-4h", "Clicks":7},
        {"Time":"4h-10h", "Clicks":5},
        {"Time":"10h-24h", "Clicks":3},
        {"Time":"24h-48h", "Clicks":2},
        {"Time":"48+", "Clicks":1}
        ]

var max = d3.max(data, function(d) { return d.Clicks; });

var barHeight = (height-40) / data.length;
var barLength = (width-65)

var x = d3.scale.linear()
    .range([0, width]);

var chart = d3.select("#barChart")
    .attr("width", width);


chart.attr("height", height );

var bar = chart.selectAll("g")
    .data(data)
    .enter().append("g")
    .attr("transform", function(d, i) { return "translate(45," + ((i * barHeight)+20) + ")"; });

bar.append("rect")
    .attr("width", function(d) { return (d.Clicks/max) * barLength; })
    .attr("fill","#3182bd") //function(d) { return d.Clicks; })
    .attr("height", barHeight - 1);

bar.append("text")
    .attr("x", function(d) { return ((d.Clicks/max) * barLength) - 10; })
    .attr("y", barHeight / 2)
    .attr("dy", ".35em")
    .attr("fill", "white")
    .attr("class", "label")
    .text(function(d) { return d.Clicks; });

bar.append("text")
    .attr("x", function(d) { return 0 - 5; })
    .attr("y", barHeight / 2)
    .attr("dy", ".35em")
    .attr("fill", "back")
    .text(function(d) { return d.Time; });
}

function update(data) {
  var barLength = (395-65)

  var max = d3.max(data, function(d) { return d.Clicks; });

  // Update labels
  var text = d3.selectAll(".label")
  .data(data);

  text.transition().attr("x", function(d) { return ((d.Clicks/max) * barLength) - 10; })
  .text(function(d) { return d.Clicks; });

  // Update lines
  d3.selectAll("rect").data(data).transition().attr("width", function(d) { return (d.Clicks/max) * barLength; });
}

$(document).ready(function() {
  function update_metrics() {
    stat_sum = $.getJSON( "/reports/stats_sum/" + window.location.href.split("=").pop(), function(sum) {
      $("#campaign_name").text(sum.campaign_name)   
      $("#active").text(sum.active)   
      $("#time").text(sum.time)   
      $("#template_name").text(sum.template)
      $("#sent").text(sum.sent)
      $("#clicked").text(sum.clicked)
      $("#opened").text(sum.opened)   
      $("#success_rate").text(Math.floor((sum.clicked/sum.sent)*100) + "%")
      $("#harvested").text(sum.harvested)
      $("#passwords").text(sum.passwords)
      $("#harvest_rate").text(Math.floor((sum.harvested/sum.sent)*100) + "%")
    
      data = [
        {"label":"Clicked", "value":sum.clicked},
        {"label":"No Action", "value":(sum.sent-(sum.clicked))} 
      ]
      change(data);
    });
  }

  update_metrics(); // invoke initial loading of metrics

  setInterval(function() {
    update_metrics();
  }, <%= (GlobalSettings.first.reports_refresh.to_f * 1000.0).to_i %>);
});
</script>
